<template>
  <!-- 基础题库 -->
  <div class="container">
    <!-- 提示 新增 -->
    <div class="prompt">
      <el-alert
        title="说明：目前支持学科和关键字条件筛选"
        type="success"
        :closable="false"
      >
      </el-alert>
      <el-button
        icon="el-icon-edit"
        size="small"
        type="success"
        @click="$router.push('new')"
      >
        新增用户
      </el-button>
    </div>
    <!-- 提示 新增 -->
    <!-- 搜索 -->
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      label-width="80px"
      size="mini"
    >
      <el-form-item label="学科">
        <el-input v-model="formInline.user" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="二级目录">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="区域一" value="shanghai"></el-option>
          <el-option label="区域二" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="无数据" value="shanghai"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键字">
        <el-input v-model="formInline.user" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="试题类型">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="难度">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="方向">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="录入人">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="题目备注">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="企业简称">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="city" label="城市">
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
        <el-select v-model="formInline.region" placeholder="请选择">
          <el-option label="启用" value="shanghai"></el-option>
          <el-option label="禁用" value="beijing"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item class="searchFor">
        <el-button size="small" @click="submit">清除</el-button>
        <el-button
          size="small"
          type="primary"
          @click="onSubmit(formInline.user)"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 搜索 -->
    <!-- 记录 -->
    <div class="record">
      <i class="el-icon-info"></i>
      <span>数据一共{{ total }} 条</span>
    </div>
    <!-- 记录 -->
    <!-- table 表格数据区域 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column prop="number" label="试题编号" width="120">
      </el-table-column>
      <el-table-column prop="subjectID" label="学科" min-width="80">
      </el-table-column>
      <el-table-column prop="catalogID" label="目录" min-width="80">
      </el-table-column>
      <el-table-column prop="questionType" label="题型" min-width="80">
      </el-table-column>
      <el-table-column prop="question" label="题干" width="280">
      </el-table-column>
      <el-table-column prop="addDate" label="录入时间" width="180">
      </el-table-column>
      <el-table-column prop="difficulty" label="难度" min-width="80">
      </el-table-column>
      <el-table-column prop="creator" label="录入人" min-width="80">
      </el-table-column>
      <el-table-column prop="address" label="操作" width="180">
        <template #default="{ row }">
          <div class="actions">
            <el-button
              type="primary"
              icon="el-icon-view"
              circle
              plain
              size="mini"
              @click="openPreview(row.id)"
            ></el-button>
            <el-button
              type="success"
              icon="el-icon-edit"
              circle
              plain
              size="mini"
              @click="$router.push(`new/${row.id}`)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              size="mini"
              @click="del(row.id)"
            ></el-button>
            <el-button
              type="warning"
              icon="el-icon-check"
              circle
              plain
              size="mini"
              @click="join(row.id)"
            >
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- table 表格数据区域 -->
    <!-- 分页组件区域 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryObj.page"
        :page-size="queryObj.pagesize"
        layout=" prev, pager, next, sizes, jumper"
        :page-sizes="[5, 10, 20, 50]"
        background
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 分页组件区域 -->

    <!-- 弹窗组件 -->
    <Preview
      ref="preview"
      :dialogFormVisible.sync="dialogVisible"
      :id="currentId"
    ></Preview>
    <!-- 弹窗组件 -->
  </div>
</template>

<script>
import { list, remove, choiceAdd } from '@/api/hmmm/questions.js'
import Preview from '../components/questions-preview.vue'
export default {
  components: {
    Preview
  },
  data () {
    return {
      formInline: {},
      list: [],
      queryObj: {
        page: 1, // 控制当前页
        pagesize: 5 // 控制每一页有几条数据
      },
      loading: false,
      dialogVisible: false, // 打开
      choiceState: 1,
      currentId: 0,
      total: 0 // 总记录数
    }
  },

  created () {
    this.initTable()
  },

  methods: {
    // 获取table数据
    async initTable () {
      this.loading = true

      const { data } = await list(this.queryObj)
      // console.log(data)

      this.list = [...data.items]
      this.total = data.counts
      // this.queryObj.page = data.page
      // this.queryObj.pageSize = data.pageSize

      this.loading = false
      console.log(this.list)
    },
    // 打开预览
    async openPreview (id) {
      this.dialogVisible = true // 显示弹层
      this.currentId = id

      // 因为vue更新是异步，在props的currentdId修改之前，已经创建了子组件发送了请求
      this.$nextTick(() => {
        // 调用子组件上的方法
        this.$refs.preview.getDepartmentInfo(id)
      })
    },
    // 点击删除图标删除对应的数据
    async del (id) {
      // console.log(id)
      // try {
      //   await remove(id)
      //   this.$message.success('删除成功')
      //   this.initTable()
      // } catch (error) {
      //   this.$message.error('删除失败')
      // }
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      })
        .then(async () => {
          await remove(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          if (this.list.length === 1 && this.queryObj.page > 1) {
            this.queryObj.page--
          }
          this.initTable()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // pageSize 改变时会触发
    handleSizeChange (pagesize) {
      // console.log(`每页 ${pagesize} 条`)
      this.queryObj.pagesize = pagesize
      this.initTable()
    },

    // current当前页改变时会触发
    handleCurrentChange (page) {
      // console.log(`当前页: ${page}`)
      // 改变参数对象中的当前页
      this.queryObj.page = page
      this.initTable()
    },
    // 加入精选题库
    async join (id) {
      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      })
        .then(async () => {
          await choiceAdd({ id: id, choiceState: 1 })
          this.$message({
            type: 'success',
            message: '加入成功!'
          })
          if (this.list.length === 1 && this.queryObj.page > 1) {
            this.queryObj.page--
          }

          this.initTable()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消加入'
          })
        })
    },
    // 清空搜索
    submit () {
      this.formInline.user = ''
      this.formInline.region = ''
      this.initTable()
    },
    // 搜索
    onSubmit (user, region) {
      // console.log(this.list.title)
      // console.log(user)
      this.list = this.list.filter(item => {
        console.log(item.title)
        return item.title === user || item.state === region
      })
    }
  }
}
</script>

<style scoped lang="less">
.container {
  padding: 20px;
  border: 1px solid #ebeef5;
  background-color: #fff;
  color: #303133;
  margin: 10px;
  .prompt {
    display: flex;
    margin-bottom: 15px;
    .el-alert--success.is-light {
      font-size: 12px;
      color: pink;
      background-color: #fff;
    }
  }
  .el-form {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    .el-form-item {
      display: flex;
      width: 25%;
      margin-right: 0;
      ::v-deep .el-form-item__content {
        display: flex;
        justify-content: space-between;
        width: 80%;
        .el-select {
          width: 100%;
          display: flex;
          justify-content: space-between;
          ::v-deep .el-input__inner {
            height: 32px;
            width: 100%;
            line-height: 32px;
            font-size: 14px;
          }
        }
      }
    }

    ::v-deep .city {
      display: flex;
      width: 25%;
      margin-right: 0;
      ::v-deep .el-form-item__content {
        display: flex;
        justify-content: space-between;
        width: 80%;
        ::v-deep .el-select {
          width: 48%;
          ::v-deep .el-input {
            height: 32px;
            line-height: 32px;
            width: 100%;
            margin-right: 10px;
            font-size: 14px;
          }
        }
      }
    }
    ::v-deep .searchFor {
      display: flex;
      justify-content: flex-end;
      width: 25%;
      padding-left: 134px;
      .el-button {
        margin-left: 10px;
      }
    }
  }
  .record {
    width: 100%;
    margin-bottom: 20px;
    background-color: #f4f4f5;
    color: #909399;
    padding: 8px 16px;
    span {
      padding: 0 8px;
      font-size: 13px;
      line-height: 18px;
    }
  }
  .block {
    display: flex;
    justify-content: flex-end;
    margin-top: 20px;
    ::v-deep .el-pagination__jump {
      margin-left: 0;
    }
  }
}
</style>
